/**展示超越好友动画 */
class GamePanel extends BasePanel
{
    	// 是否已经检测过超越好友排名
	private haveCheck:boolean = false;
    public userInfo:any = null;
    public updateIndex = 0;
    public updateDoneIndex = 0;
    public constructor(data:any,index: number = GameConfig.uiIndex.over)
    {
        super(data,index);
        this.userInfo = data.mine;
        this.data = data;
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.createChildren,this);
    }
    public createChildren()
    {   
        this.updateInfo();
    }
    public updateInfo()
    {
        GlobalData.getAllFriendData(this.userInfo,this.showRank.bind(this));
        // 检测是否超越好友排名
        this.checkCrossFriendRank();
    }
    /**
     * 将排行榜数据展示出来
     */
    public showRank(success:boolean,list: Array<any>) {
        //防止第一次打开没数据
        if (list && list.length) {
            // 检测是否超越好友排名
            this.checkCrossFriendRank();
        }
    }

    /**
     * 检测是否超越好友排名
     */
    private lastShowedCrossNextId:any = 0;
    public checkCrossFriendRank() {
        
        if(!GlobalData.friendDataList || !GlobalData.friendDataList.length)return;
        if(this.updateIndex != this.updateDoneIndex)return;
        this.updateDoneIndex = this.updateIndex+1;
        // 获取当前用户的相关信息、获取下一个用户的信息
        let mineScore:number = this.userInfo.score;
        // 获取当前用户的信息
        let mineRankData:UserInfo = null;
        for(let i=0;i<GlobalData.friendDataList.length;i++)
        {
            if(GlobalData.friendDataList[i].id == this.userInfo.id){
                mineRankData = GlobalData.friendDataList[i];
            }
        }
        // 所超越的下一个好友的分数的索引
        let nextIndex = this.checkNext(mineScore);
        if(nextIndex == -1){
             // 没有数据或比任何好友的分数都小
        }else{
            let nextRankData = GlobalData.friendDataList[nextIndex];
            if(nextRankData.id == this.userInfo.id){
                // 超越的是自己，不做处理
            }else if(nextIndex == 0){
                // 超越了最高分，提示成功登顶
                if(this.lastShowedCrossNextId == nextRankData.id){
                    // 已经提示过登顶
                    return;
                }else{
                    this.lastShowedCrossNextId = nextRankData.id
                }
                this.successToRankFirst(mineRankData);
            }else{
                if(this.lastShowedCrossNextId == nextRankData.id){
                    // 超越的还是之前的提示过的那个好友
                    return;
                }else{
                    // 超越某个好友
                    this.lastShowedCrossNextId = nextRankData.id;
                    if(parseInt(nextRankData.highestScore)<=10){
                        return;
                    }
                    this.successToCrossNext(mineRankData,nextRankData);
                }
                
            }
        }
    }

    /**
     * 成功登顶
     */
    private successToRankFirst(mineRankData:any)
    {
        if(this.userInfoGroup){
            egret.Tween.removeTweens(this.userInfoGroup);
        }
        if(this.friendGroup){
            egret.Tween.removeTweens(this.friendGroup);
        }
        // 没有创建展示用户信息的相关内容，展示内容
        if(!this.userInfoGroup){
            this.setupUserInfoGroup();
        }
        if(!this.contains(this.userInfoGroup)){
            this.addChild(this.userInfoGroup);
        }
        if(this.contains(this.friendGroup)){
            this.removeChild(this.friendGroup);
        }
        this.userScoreText.visible = false;
        this.rankFirstIcon.visible = true;
        
        // 设置当前用户头像
        let userAvatarImageLoader = new egret.ImageLoader();
        userAvatarImageLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
            let loader = <egret.ImageLoader>event.currentTarget;
            let bgtexture = new egret.Texture();
            bgtexture._setBitmapData(loader.data);
            this.userAvatarImg.texture = bgtexture;
        }, this);
        userAvatarImageLoader.load(mineRankData.avatarUrl);     
        this.userInfoGroup.x = GameConfig.stageWidth - this.userInfoGroup.width-160;

         // 1.5秒后删除好友模块
        egret.Tween.get(this.userInfoGroup).wait(1500).call(() => {
           // this.removeChild(this.userInfoGroup);
        })
    }
    /**
     * 成功超越下一位的分数
     */
    private successToCrossNext(mineRankData:UserInfo,nextRankData:UserInfo)
    {
        // if(this.userInfoGroup){
        //     egret.Tween.removeTweens(this.userInfoGroup);
        // }
        if(this.friendGroup){
            egret.Tween.removeTweens(this.friendGroup);
        }
         // 当前用户信息模块
        // if(!this.userInfoGroup){
        //     this.setupUserInfoGroup();
        // }
        
        // if(!this.contains(this.userInfoGroup)){
        //     this.addChild(this.userInfoGroup);
        // }
        // egret.Tween.get(this.userInfoGroup).wait(3000).call(() => {
        //     this.removeChild(this.userInfoGroup);
        // });
        /*
        this.userScoreText.visible = true;
        this.rankFirstIcon.visible = false;
         // 设置当前用户头像
        let userAvatarImageLoader = new egret.ImageLoader();
        userAvatarImageLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
            let loader = <egret.ImageLoader>event.currentTarget;
            let bgtexture = new egret.Texture();
            bgtexture._setBitmapData(loader.data);
            this.userAvatarImg.texture = bgtexture;
        }, this);
        userAvatarImageLoader.load(mineRankData.avatarUrl); 
        this.userScoreText.text = this.userInfo.score;
         // 用户模块竖直方向上往上滚动
        // 用户模块从147下面64处移动到64
        let sy = 147;
        this.userInfoGroup.y = sy + 64;
        this.userInfoGroup.x = GameConfig.stageWidth - this.userAvatarImg.width - this.userScoreText.width-40;
        egret.Tween.get(this.userInfoGroup).to({ y: sy }, 1500);
        */

        // 被超越用户信息模块
        if(!this.friendGroup){
            this.setupCrossFriendGroup();
        }
        
        // 设置好友头像
        let friendAvatarImageLoader = new egret.ImageLoader();
        friendAvatarImageLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
            let loader = <egret.ImageLoader>event.currentTarget;
            let bgtexture = new egret.Texture();
            bgtexture._setBitmapData(loader.data);
            this.friendAvatarImg.texture = bgtexture;
        }, this);
        friendAvatarImageLoader.load(nextRankData.avatarUrl);

        // 设置好友昵称
        this.friendNameTxt.text = ParseUtils.getInstance().parseName(nextRankData.nickname);
        // 设置好友分数
        this.friendScoreTxt.text = nextRankData.highestScore;
        let totalFriendInfoWidth = this.friendAvatarImg.width+this.friendNameTxt.width+this.friendScoreTxt.width+20;
        this.friendNameTxt.x = this.friendAvatarImg.x + this.friendAvatarImg.width+10;
        this.friendScoreTxt.x = this.friendNameTxt.x + this.friendNameTxt.width+10;

        this.friendContainer.width = totalFriendInfoWidth;
        this.friendContainer.x = (this.friendGroup.width - totalFriendInfoWidth)/2;

        // 如果没有好友信息，则添加好友模块信息
        if (this.contains(this.friendGroup)) {
            this.removeChild(this.friendGroup);
        }

        // 1.5秒后删除好友模块
        egret.Tween.get(this.friendGroup).wait(0).call(() => {
            this.addChild(this.friendGroup);
        }).wait(1500).call(()=>{
            this.removeChild(this.friendGroup);
        });

    }

    
    /**被超越的朋友的容器 */
    private friendGroup: egret.DisplayObjectContainer;
    // 超越好友图片
    private crossFriendIcon: egret.Bitmap;
    private friendWhiteCirleShape: egret.Shape;
    private friendAvatarMask: egret.Shape;
    /**被超越好友头像 */
    private friendAvatarImg: egret.Bitmap;
    /**被超越好友姓名 */
    private friendNameTxt: egret.TextField;
    /**被超越好友分数 */
    private friendScoreTxt: egret.TextField;

    private friendContainer: egret.DisplayObjectContainer;
    /**
     * 超越好友部分
     */
    private setupCrossFriendGroup() {
        this.friendGroup = new egret.DisplayObjectContainer();
        this.friendGroup.width = 636;

        this.friendContainer = new egret.DisplayObjectContainer();
        this.friendGroup.addChild(this.friendContainer);

        let by =  550;
        this.friendGroup.y = by;
        this.friendContainer.y = 70;
        this.friendContainer.width = GameConfig.stageWidth;

         // 超越好友图片
        this.crossFriendIcon = new egret.Bitmap();
        this.crossFriendIcon.x = 228;
        this.friendGroup.addChild(this.crossFriendIcon);
        let crossImageLoader = new egret.ImageLoader();
        crossImageLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
            let loader = <egret.ImageLoader>event.currentTarget;
            let texture = new egret.Texture();
            texture._setBitmapData(loader.data);
            this.crossFriendIcon.texture = texture;
        }, this);
        crossImageLoader.load("resource/assets/game/beatIcon.png");

        /**超越好友部分头像：白色边框圆圈+好友圆圈头像 */
        //bg
        this.friendWhiteCirleShape = new egret.Shape();
        this.friendWhiteCirleShape.graphics.beginFill(0xffffff);
        this.friendWhiteCirleShape.graphics.drawCircle(22, 22, 22);
        this.friendWhiteCirleShape.graphics.endFill();
        this.friendContainer.addChild(this.friendWhiteCirleShape);
        //头像
        this.friendAvatarMask = new egret.Shape();
        this.friendAvatarMask.graphics.beginFill(0x000000);
        this.friendAvatarMask.graphics.drawCircle(22, 22, 20);
        this.friendAvatarMask.graphics.endFill();
        this.friendContainer.addChild(this.friendAvatarMask);
        // 好友头像
        this.friendAvatarImg = new egret.Bitmap();
        this.friendAvatarImg.width = 40;
        this.friendAvatarImg.height = 40;
        this.friendAvatarImg.mask = this.friendAvatarMask;
        this.friendAvatarImg.y = 2;
        this.friendAvatarImg.x = 2;
        this.friendContainer.addChild(this.friendAvatarImg);

        // 好友名称
        this.friendNameTxt = new egret.TextField();
        this.friendNameTxt.x = 52;
        this.friendNameTxt.size = 26;
        this.friendNameTxt.y = 9;
        this.friendContainer.addChild(this.friendNameTxt);

        // 好友分数
        this.friendScoreTxt = new egret.TextField();
        this.friendScoreTxt.size = 32;
        this.friendScoreTxt.y = 5.5;
        this.friendContainer.addChild(this.friendScoreTxt);
        this.friendGroup.x = 57;

    }

    
    /**设置当前用户的头像分数等信息*/
    private userInfoGroup:egret.DisplayObjectContainer;
    /**用户头像*/
    private userAvatarImg:egret.Bitmap;
    /**用户分数*/
    private userScoreText:egret.TextField;
    /**成功登顶图片*/
    private rankFirstIcon: egret.Bitmap;
    /**
     * 设置用户头像分数成功登顶等信息
     */
    private setupUserInfoGroup() {
        // 包含当前用户头像分数/成功登顶的容器
        this.userInfoGroup = new egret.DisplayObjectContainer();
        this.addChild(this.userInfoGroup);
        let sy = 147;
        this.userInfoGroup.y = sy;
        this.userInfoGroup.height = 64;

        /**滚动模块 */
        let mask = new egret.Shape();
        mask.graphics.beginFill(0x000000);
        mask.graphics.drawRect(0, this.userInfoGroup.y, 750, this.userInfoGroup.height);
        mask.graphics.endFill();
        this.userInfoGroup.mask = mask;

        /*头像部分：一圈白色的外边，头像，让头像是圆形*/
        //白色边框的圆圈
        let whiteCircleBg = new egret.Shape();
        whiteCircleBg.graphics.beginFill(0xffffff);
        whiteCircleBg.graphics.drawCircle(32, 32, 32);
        whiteCircleBg.graphics.endFill();
        this.userInfoGroup.addChild(whiteCircleBg);

        //头像展示成圆的mask
        let userAvatarMask = new egret.Shape();
        userAvatarMask.graphics.beginFill(0x000000);
        userAvatarMask.graphics.drawCircle(32, 32, 30);
        userAvatarMask.graphics.endFill();
        this.userInfoGroup.addChild(userAvatarMask);

        // 头像图片
        this.userAvatarImg = new egret.Bitmap();
        this.userAvatarImg.y = 0;
        this.userAvatarImg.width = 60;
        this.userAvatarImg.height = 60;
        this.userAvatarImg.mask = userAvatarMask;
        this.userInfoGroup.addChild(this.userAvatarImg);

        
        // 成功登顶的图片
        this.rankFirstIcon = new egret.Bitmap();
        this.rankFirstIcon.y = 5.5;
        this.rankFirstIcon.x = this.userAvatarImg.x + this.userAvatarImg.width + 22;
        this.userInfoGroup.addChild(this.rankFirstIcon);
        // 加载成功登顶的图片
        let rankFirstImageLoader = new egret.ImageLoader();
        rankFirstImageLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
            let loader = <egret.ImageLoader>event.currentTarget;
            let bgtexture = new egret.Texture();
            bgtexture._setBitmapData(loader.data);
            this.rankFirstIcon.texture = bgtexture;
        }, this);
        rankFirstImageLoader.load("resource/assets/game/rankMax.png");
        this.rankFirstIcon.visible = false;

        /**分数 */
        this.userScoreText = new egret.TextField();
        this.userScoreText.x = this.userAvatarImg.x + this.userAvatarImg.width + 22;
        this.userScoreText.size = 36;
        this.userScoreText.y = 14;
        this.userInfoGroup.addChild(this.userScoreText);
        this.userScoreText.visible = false;
        
    }
    /**
     * 检查当前分数所对应超过的排名
     */
    private checkNext(mineScore:number) {
        let nextIndex = -1;
        for (let i = 0; i < GlobalData.friendDataList.length; i++) {
            let item = GlobalData.friendDataList[i];
            if(mineScore>parseInt(item.highestScore)){
                nextIndex = i;
                break;
            }
        }
        return nextIndex;
    }
    
}